﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace Card.Web.ImageService.Helper
{
    internal class ImageHelper
    {
        public static Font GetFont(string familyCode, float size, string fontStyle)
        {
            BAF.Entity.LookupValue config = Common.LookUpCache.GetLookupValue(CONST.LOOKUP_TYPE.FONT_FAMILY, familyCode);
            FontStyle style = FontStyle.Regular;
            string[] styles = fontStyle.Split(',');
            foreach (string s in styles)
            {
                switch (s)
                {
                    case CONST.FontStyle.Bold:
                        style = style | FontStyle.Bold;
                        break;
                    case CONST.FontStyle.Italic:
                        style = style | FontStyle.Italic;
                        break;
                    case CONST.FontStyle.Strikeout:
                        style = style | FontStyle.Strikeout;
                        break;
                    case CONST.FontStyle.Underline:
                        style = style | FontStyle.Underline;
                        break;
                }
            }


            return new Font(config.Value, size, style, GraphicsUnit.Pixel);
        }

        /// <summary>
        /// 得到图片缩略图
        /// </summary>
        /// <param name="sourceImage">源图片</param>
        /// <param name="thumbailImagePath">保存路径</param>
        /// <param name="imgExtension">图片扩展名</param>
        /// <param name="imageWidth">图片宽度</param>
        /// <param name="imageHeight">图片高度</param>
        /// <param name="absolute">是否按长宽绝对缩放</param>
        public static void ToThumbnail(Image sourceImage, string fullName, string suffix, int imageWidth, int imageHeight, bool absolute)
        {

            if (absolute)
            {
                using (System.Drawing.Image bitmap = new Bitmap(imageWidth, imageHeight))
                {
                    Graphics graph = Graphics.FromImage(bitmap);

                    graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
                    graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                    graph.Clear(Color.Blue);

                    graph.DrawImage(sourceImage,
                        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                        new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                        GraphicsUnit.Pixel);

                    //如果存在则删除原图
                    if (File.Exists(fullName))
                    {
                        File.Delete(fullName);
                    }

                    bitmap.Save(fullName, GetImageFormat(suffix));
                }
            }
            else
            {
                int width = imageWidth;
                int height = imageHeight;

                //计算图片的比例
                if (sourceImage.Width > imageWidth || sourceImage.Height > sourceImage.Height)
                {
                    if ((double)sourceImage.Width / (double)width > (double)sourceImage.Height / (double)height)
                    {
                        height = Convert.ToInt32((double)sourceImage.Height * (double)width / (double)sourceImage.Width);
                        if (height > imageHeight)
                        {
                            width = Convert.ToInt32((double)width * (double)imageHeight / (double)height);
                            height = imageHeight;
                        }
                    }
                    else
                    {
                        width = Convert.ToInt32((double)sourceImage.Width * (double)height / (double)sourceImage.Height);
                        if (width > imageWidth)
                        {
                            height = Convert.ToInt32((double)width * (double)imageWidth / (double)width);
                            width = imageWidth;
                        }
                    }

                    //保存等比图片
                    using (System.Drawing.Image bitmap = new Bitmap(width, height))
                    {
                        Graphics graph = Graphics.FromImage(bitmap);

                        graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                        graph.Clear(Color.Blue);

                        graph.DrawImage(sourceImage,
                            new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                            new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                            GraphicsUnit.Pixel);

                        //如果存在则删除原图
                        if (File.Exists(fullName))
                        {
                            File.Delete(fullName);
                        }

                        bitmap.Save(fullName, GetImageFormat(suffix));
                    }
                }
                else
                {
                    sourceImage.Save(fullName);
                }
            }
        }

        /// <summary>
        /// 获得图片格式
        /// </summary>
        /// <param name="extendName"></param>
        /// <returns></returns>
        private static ImageFormat GetImageFormat(String extendName)
        {
            extendName = extendName.ToLower();
            if (extendName == "gif")
            {
                return ImageFormat.Gif;
            }
            else if (extendName == "jpg" || extendName == "jpeg")
            {
                return ImageFormat.Jpeg;
            }
            else if (extendName == "bmp")
            {
                return ImageFormat.Bmp;
            }
            else if (extendName == "wmf")
            {
                return ImageFormat.Emf;
            }
            else if (extendName == "exif")
            {
                return ImageFormat.Exif;
            }
            else if (extendName == "icon")
            {
                return ImageFormat.Icon;
            }
            else if (extendName == "tiff")
            {
                return ImageFormat.Tiff;
            }
            else
            {
                return ImageFormat.Png;
            }
        }
        /// <summary>
        /// 得到模版图片绝对路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetTemplateImageFullName(string fileName)
        {
            return HttpContext.Current.Server.MapPath(string.Format("TemplateImages/{0}/{1}/{2}", fileName.Substring(0, 4), fileName.Substring(4, 2), fileName));
        }

        /// <summary>
        /// 得到模版图片绝对路径
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetLogoFullName(string fileName)
        {
            return HttpContext.Current.Server.MapPath(string.Format("logoImages/{0}", fileName));
        }
    }
}
